package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwggT;
import cn.e4j.modular.crm.entity.GwghT;
import cn.e4j.modular.crm.mapper.GwggTMapper;
import cn.e4j.modular.crm.service.GwggTService;
import cn.e4j.modular.system.mapper.SysOrgMapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  竞争分析 服务实现类
 * </p>
 *
 * @author miaoyl
 * @since 2019-11-22
 */
@Service
public class GwggTServiceImpl extends ServiceImpl<GwggTMapper, GwggT> implements GwggTService {

    @Autowired
    private SysAuthorityService sysAuthorityService;

    @Autowired
    SysOrgMapper sysOrgMapper;

    @Override
    public List<Map<String, Object>> getGwggList(Page<Object> page, Dto inDto) {
        ShiroUser shiroUser = ShiroKit.getUser();
        String ORG_ID = shiroUser.getORG_ID();
//        if(sysAuthorityService.getNolimitOrg(ORG_ID,null) == null ){
//            //客户权限
//            inDto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(inDto));
//            //部门权限
//            List<Dto> orgs = sysAuthorityService.getOrgsAuthority(inDto);
//            if(orgs.size() > 0) {
//                orgs.removeIf(orgDto -> orgDto.getString("ID").equals(ORG_ID));
//            }
//            inDto.put("CURR_USER",shiroUser.getID());
//            inDto.put("ORGS", orgs);
//        }
        List<Map<String, Object>> gwggDtos = this.baseMapper.getGwggList(page, inDto);
        return gwggDtos;
    }

    @Override
    @Transactional(readOnly = true)
    public Dto getAddData(Dto dto){
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        outDto.put("GWGGORIG", shiroUser.getORG_ID());
        outDto.put("GWGGORIU", shiroUser.getID());
        outDto.put("GWGGORIU_NAME", shiroUser.getNAME());
        outDto.put("GWGGORIG_NAME", shiroUser.getSysOrgT().getNAME());
        return outDto;
    }

    @Override
    public List<Map<String, Object>> getGwgdData(Dto dto) {
        List<Map<String, Object>> gwgdDtos = this.baseMapper.getGwgdData(dto);
        return gwgdDtos;
    }

    @Override
    public List<Map<String, Object>> getGwghData(Dto dto) {
        List<Map<String, Object>> gwghDtos = this.baseMapper.getGwghData(dto);
        return gwghDtos;
    }

    @Override
    @Transactional
    public void addData(List<GwghT> gwghTList, GwggT gwggT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Integer GWGGSEQ = this.baseMapper.getMaxGwggseq(gwggT.getGWGGID());
        if(GWGGSEQ == null) {
            GWGGSEQ = 1;
        }else {
            GWGGSEQ = GWGGSEQ + 1;
        }
        //竞争产品明细新增
        for(GwghT gwghT : gwghTList){
            gwghT.setGWGHENT(shiroUser.getDBC());
            gwghT.setGWGHID(gwggT.getGWGGID());
            gwghT.setGWGHSEQ(GWGGSEQ);
            gwghT.setGWGH001(gwggT.getGWGG001());
            gwghT.setGWGHUSER(shiroUser.getID());
            gwghT.setGWGHGRUP(shiroUser.getORG_ID());
            gwghT.setGWGHTIME(DateUtils.formatDate(new Date(),"HH:mm:ss"));
            gwghT.setGWGHORIG(shiroUser.getORG_ID());
            gwghT.setGWGHORIU(shiroUser.getID());
            gwghT.setGWGHDATE(LocalDate.now());
            gwghT.insert();
        }

        gwggT.setGWGGSEQ(GWGGSEQ);
        gwggT.setGWGGENT(shiroUser.getDBC());
        gwggT.setGWGGUSER(shiroUser.getORG_ID());
        gwggT.setGWGGGRUP(shiroUser.getID());
        gwggT.setGWGGTIME(DateUtils.formatDate(new Date(),"HH:mm:ss"));
        gwggT.setGWGGDATE(LocalDate.now());
        gwggT.setGWGGACTI("N");
        gwggT.insert();
    }

    @Override
    @Transactional
    public void editData(List<GwghT> gwghTList, GwggT gwggT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwghT gwghTDel = new GwghT();
        UpdateWrapper<GwghT> gwghTUpdateWrapper = new UpdateWrapper<>();
        gwghTUpdateWrapper.eq("GWGHENT", shiroUser.getDBC());
        gwghTUpdateWrapper.eq("GWGHSEQ", gwggT.getGWGGSEQ());
        gwghTUpdateWrapper.eq("GWGHID", gwggT.getGWGGID());
        gwghTDel.delete(gwghTUpdateWrapper);

        //竞争产品明细新增
        for(GwghT gwghT : gwghTList){
            gwghT.setGWGHENT(shiroUser.getDBC());
            gwghT.setGWGHID(gwggT.getGWGGID());
            gwghT.setGWGHSEQ(gwggT.getGWGGSEQ());
            gwghT.setGWGH001(gwggT.getGWGG001());
            gwghT.setGWGHUSER(shiroUser.getID());
            gwghT.setGWGHGRUP(shiroUser.getORG_ID());
            gwghT.setGWGHTIME(DateUtils.formatDate(new Date(),"HH:mm:ss"));
            gwghT.setGWGHORIG(shiroUser.getORG_ID());
            gwghT.setGWGHORIU(shiroUser.getID());
            gwghT.setGWGHDATE(LocalDate.now());
            gwghT.insert();
        }

        gwggT.setGWGGMODU(shiroUser.getID());
        UpdateWrapper<GwggT> gwggTUpdateWrapper = new UpdateWrapper<>();
        gwggTUpdateWrapper.eq("GWGGENT", shiroUser.getDBC());
        gwggTUpdateWrapper.eq("GWGGID", gwggT.getGWGGID());
        gwggTUpdateWrapper.eq("GWGGSEQ", gwggT.getGWGGSEQ());
        gwggT.update(gwggTUpdateWrapper);
    }

    @Override
    @Transactional
    public void deleteData(GwggT gwggT) {
        GwghT gwghT = new GwghT();
        UpdateWrapper<GwghT> gwghTUpdateWrapper = new UpdateWrapper<>();
        gwghTUpdateWrapper.eq("GWGHENT", gwggT.getGWGGENT());
        gwghTUpdateWrapper.eq("GWGHID", gwggT.getGWGGID());
        gwghTUpdateWrapper.eq("GWGHSEQ", gwggT.getGWGGSEQ());

        UpdateWrapper<GwggT> gwggTUpdateWrapper = new UpdateWrapper<>();
        gwggTUpdateWrapper.eq("GWGGENT", gwggT.getGWGGENT());
        gwggTUpdateWrapper.eq("GWGGID", gwggT.getGWGGID());
        gwggTUpdateWrapper.eq("GWGGSEQ", gwggT.getGWGGSEQ());

        gwghT.delete(gwghTUpdateWrapper);
        gwggT.delete(gwggTUpdateWrapper);
    }

    @Override
    @Transactional
    public void delDataBatch(List<GwggT> gwggTList) {
        for(GwggT gwggT : gwggTList) {
            GwghT gwghT = new GwghT();
            UpdateWrapper<GwghT> gwghTUpdateWrapper = new UpdateWrapper<>();
            gwghTUpdateWrapper.eq("GWGHENT", gwggT.getGWGGENT());
            gwghTUpdateWrapper.eq("GWGHID", gwggT.getGWGGID());
            gwghTUpdateWrapper.eq("GWGHSEQ", gwggT.getGWGGSEQ());

            UpdateWrapper<GwggT> gwggTUpdateWrapper = new UpdateWrapper<>();
            gwggTUpdateWrapper.eq("GWGGENT", gwggT.getGWGGENT());
            gwggTUpdateWrapper.eq("GWGGID", gwggT.getGWGGID());
            gwggTUpdateWrapper.eq("GWGGSEQ", gwggT.getGWGGSEQ());

            gwghT.delete(gwghTUpdateWrapper);
            gwggT.delete(gwggTUpdateWrapper);
        }
    }

    @Transactional
    @Override
    public void updateStatus(List<GwggT> gwggTList) {
        for (GwggT gwggT : gwggTList) {
            UpdateWrapper<GwggT> gwggTUpdateWrapper = new UpdateWrapper<>();
            gwggTUpdateWrapper.eq("GWGGENT", gwggT.getGWGGENT());
            gwggTUpdateWrapper.eq("GWGGID", gwggT.getGWGGID());
            gwggTUpdateWrapper.eq("GWGGSEQ", gwggT.getGWGGSEQ());
            gwggT.update(gwggTUpdateWrapper);
        }
    }
}
